//
//  NineImageTableViewCell.m
//  GoldBook
//
//  Created by wmz on 2021/9/6.
//  Copyright © 2021 wmz. All rights reserved.
//

#import "NineImageTableViewCell.h"
#import "YBImageBrowser.h"
@interface NineImageTableViewCell()
@property (nonatomic, strong) UIView *imageContainerView;
@property (nonatomic, strong) NSMutableArray <UIImageView*>*imageArr;
@end
@implementation NineImageTableViewCell

- (void)hd_setupViews{
    for (int i = 0; i < 9; i++) {
        UIImageView *tempImageView = [UIImageView new];
        tempImageView.hidden = YES;
        tempImageView.contentMode = UIViewContentModeScaleAspectFill;
        tempImageView.clipsToBounds = YES;
        tempImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTheImageView:)];
        [tempImageView addGestureRecognizer:tap];
        [self.imageContainerView addSubview:tempImageView];
        [self.imageArr addObject:tempImageView];
    }
    [self.contentView addSubview:self.imageContainerView];
}

- (void)tapTheImageView:(UITapGestureRecognizer*)ta{
    [self showIndex:ta.view.tag - 10000];
}

- (void)setGNModel:(BaseCellModel*)data{
    self.model = data;
    data.notCacheHeight = YES;
    UIImageView *lastImageView = nil;
    CGFloat itemW =  floor((PostWidth - 30)/3.0);
    CGFloat itemH = itemW;
    NSInteger count = self.imageArr.count;
    NSInteger currentCount = data.dataSource.count;
    for (NSInteger i = currentCount; i < count ; i++) {
        self.imageArr[i].hidden = YES;
    }
    for (NSInteger i = 0; i < currentCount; i++) {
        id model = data.dataSource[i];
        UIImageView *imageView = self.imageArr[i];
        imageView.hidden = NO;
        if ([model isKindOfClass:NSString.class]) {
            NSString *modelStr = (NSString*)model;
            if ([modelStr hasPrefix:@"http"]) {
                [imageView sd_setImageWithURL:[NSURL URLWithString:modelStr] placeholderImage:PostDefaultIcon];
            }else{
                imageView.image = [UIImage imageNamed:modelStr];
            }
        }else if ([model isKindOfClass:UIImage.class]){
            imageView.image = (UIImage*)model;
        }else if ([model isKindOfClass:BaseCellModel.class]){
            BaseCellModel *baseModel = (BaseCellModel*)model;
            if ([baseModel.imageTitle hasPrefix:@"http"]) {
                [imageView sd_setImageWithURL:[NSURL URLWithString:baseModel.imageTitle] placeholderImage:PostDefaultIcon];
            }else{
                imageView.image = [UIImage imageNamed:baseModel.imageTitle];
            }
        }
        [imageView setTag:10000+i];
        NSInteger section = (i+1)%3;
        CGRect rect = CGRectZero;
        if (!lastImageView) {
            rect = CGRectMake(0, 0, itemW, itemH);
        }else{
            if (section == 1) {
                rect = CGRectMake(0, CGRectGetMaxY(lastImageView.frame)+15, itemW, itemH);
            }else{
                rect = CGRectMake(CGRectGetMaxX(lastImageView.frame)+15, CGRectGetMinY(lastImageView.frame), itemW, itemH);
            }
        }
        imageView.frame = rect;
        lastImageView = imageView;
    }
    self.imageContainerView.frame = CGRectMake(13,15, PostWidth - 26, CGRectGetMaxY(lastImageView.frame)+15);
    data.cellHeight = CGRectGetMaxY(self.imageContainerView.frame);
    self.lineView.hidden = NO;
}

- (void)showIndex:(NSInteger)index{
    NSMutableArray <YBIBImageData*>*marr = [NSMutableArray new];
    for (id model in self.model.dataSource) {
        YBIBImageData *data = [YBIBImageData new];
        if ([model isKindOfClass:NSString.class]) {
            NSString *modelStr = (NSString*)model;
            data.imageURL = [NSURL URLWithString:modelStr];
        }else if ([model isKindOfClass:UIImage.class]){
            data.image = ^UIImage * _Nullable{
                return (UIImage*)model;
            };
        }else if ([model isKindOfClass:BaseCellModel.class]){
            BaseCellModel *baseModel = (BaseCellModel*)model;
            if ([baseModel.imageTitle isKindOfClass:NSString.class]) {
                data.imageURL = [NSURL URLWithString:baseModel.imageTitle];
            } else if ([baseModel.imageTitle isKindOfClass:UIImage.class]) {
                data.image = ^UIImage * _Nullable{
                    return (UIImage*)baseModel.imageTitle;
                };
            }
        }
        [marr addObject:data];
    }
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = marr;
    browser.autoHideProjectiveView = false;
    browser.currentPage = index;
    [browser show];
}

- (UIView *)imageContainerView{
    if (!_imageContainerView) {
        _imageContainerView = UIView.new;
    }
    return _imageContainerView;
}

- (NSMutableArray<UIImageView *> *)imageArr{
    if (!_imageArr) {
        _imageArr = NSMutableArray.new;
    }
    return _imageArr;
}
@end
